package com.beyond.util;

import org.apache.commons.lang3.StringUtils;

public class ManagerSqlUtil {

	private static StringBuilder builder = new StringBuilder();
	
	/**
	 * 返回求和字段的sql
	 * @param strs
	 * @param flag
	 * @return
	 */
	public static String assembSumCounterSql(String cols){
		StringBuilder sb = new StringBuilder();
		if(StringUtils.isBlank(cols))
			return null;
			
		String[] strArray = cols.split(",");
		for (String str : strArray) {
				sb.append(" SUM(t.").append(str).append("),");
		}
		
		return sb.substring(0,sb.length()-1);
	}
	
	/**
	 * 字段添加标示和给时间维字段添加Distinc唯一标示
	 * @param cols
	 * @return
	 */
	public static String assembPrefixTarget(String cols){
		if(StringUtils.isBlank(cols))
			return null;
			
		String[] strArray = cols.split(",");
		for (String str : strArray) {
			if("hour_id".equals(str.toLowerCase()) || "minute_15_id".equals(str.toLowerCase()) 
					|| "minute_30_id".equals(str.toLowerCase()) || "day_id".equals(str.toLowerCase())){
				builder.append(" distinct t.").append(str).append(",");
			}else{
				builder.append(" t.").append(str).append(",");
			}
		}
		
		return builder.substring(0,builder.length()-1);
	}
	
	/**
	 * where添加判断条件
	 * @param strs
	 * @return
	 */
	public static String assembConditionSql(String cols){
		if(StringUtils.isBlank(cols))
			return null;
		
		String[] strArray = cols.split(",");
		for (String str : strArray) {
			builder.append(" t.").append(str).append(" = ").append(" a.").append(str).append(" and");
		}
		
		return builder.substring(0,builder.length()-3);
	}
	
	/**
	 *  组装维度或指标为-1 || 0 的sql
	 * @param sql
	 * @return
	 */
	public static String assembBelow1Sql(String cols){
		if(StringUtils.isBlank(cols))
			return null;
		
		String[] dimSql = cols.split(",");
		for (String dim : dimSql) {
			// SELECT COUNT((CASE terminal_oid WHEN -1 THEN -1 WHEN 0 THEN 1 END ))
			builder.append("COUNT ((CASE ").append(dim).append(" WHEN -1 THEN -1 WHEN 0 THEN 1 END))").append(dim).append(",");
		}
		
		return builder.substring(0,builder.length()-1);
	}
	
}